import os
from PIL import Image
import matplotlib.pyplot as plt

# 替换为你的图像文件夹路径
image_folder = r"E:\datasets\11"

# 获取所有图片文件名（可根据需要排序）
image_files = sorted([
    f for f in os.listdir(image_folder)
    if f.lower().endswith(('.png', '.jpg', '.jpeg'))
])

# 控制显示帧数（比如只取前 6 张）
num_frames_to_show = 6
image_files = image_files[:num_frames_to_show]

# 加载图像序列
img_seq = [Image.open(os.path.join(image_folder, f)).convert("RGBA") for f in image_files]

# 获取图像尺寸
img_width, img_height = img_seq[0].size

# 设置堆叠偏移
x_spacing = 15
y_offset = 10

# 创建透明背景画布
canvas_width = img_width + x_spacing * (len(img_seq) - 1)
canvas_height = img_height + y_offset * (len(img_seq) - 1)
canvas = Image.new('RGBA', (canvas_width, canvas_height), (255, 255, 255, 0))

# 逐帧粘贴图像
for i, img in enumerate(img_seq):
    x = i * x_spacing
    y = (len(img_seq) - i - 1) * y_offset
    canvas.paste(img, (x, y), img)

# 显示最终效果
plt.figure(figsize=(6, 6))
plt.imshow(canvas)
plt.axis('off')
# plt.title(r"$X_{img}$ (N × H × W)")
plt.show()




# import os
# from PIL import Image, ImageDraw, ImageFont
# import matplotlib.pyplot as plt
#
# # 替换为你的图像文件夹路径
# image_folder = r"E:\datasets\CAISA-B\001\bg-01\018"
#
# # 加载并排序图像文件
# image_files = sorted([
#     f for f in os.listdir(image_folder)
#     if f.lower().endswith(('.png', '.jpg', '.jpeg'))
# ])
#
# # 指定要展示的帧数（前两帧 + 后两帧）
# num_frames_each_side = 2
# image_files = image_files[:num_frames_each_side] + image_files[-num_frames_each_side:]
#
# # 加载图像（转为RGBA支持透明度）
# img_seq = [Image.open(os.path.join(image_folder, f)).convert("RGBA") for f in image_files]
#
# # 图像大小
# img_width, img_height = img_seq[0].size
#
# # 设置偏移：X小、Y大 → 模拟透视堆叠
# x_spacing = 25
# y_offset = 30
# n_imgs = len(img_seq)
# n_total = n_imgs + 1  # 加一个省略号占位
#
# # 画布尺寸
# canvas_width = img_width + x_spacing * (n_total - 1)
# canvas_height = img_height + y_offset * (n_total - 1)
# canvas = Image.new('RGBA', (canvas_width, canvas_height), (255, 255, 255, 0))
#
# # 把前两帧贴上去
# for i in range(num_frames_each_side):
#     x = i * x_spacing
#     y = (n_total - i - 1) * y_offset
#     canvas.paste(img_seq[i], (x, y), img_seq[i])
#
# # 添加省略号（...）
# draw = ImageDraw.Draw(canvas)
# dot_x = num_frames_each_side * x_spacing + img_width // 2
# dot_y = (n_total - num_frames_each_side - 1) * y_offset + img_height // 2
# draw.text((dot_x, dot_y), "...", fill="black")
#
# # 把后两帧贴上去
# for j in range(num_frames_each_side, n_imgs):
#     i = j + 1  # 因为中间加了省略号
#     x = i * x_spacing
#     y = (n_total - i - 1) * y_offset
#     canvas.paste(img_seq[j], (x, y), img_seq[j])
#
# # 显示结果
# plt.figure(figsize=(6, 6))
# plt.imshow(canvas)
# plt.axis('off')
# plt.title(r"$X_{img}$ (N × H × W)")
# plt.show()
